$(function(){
	$.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
    var loginInfo = JSON.parse(localStorage.getItem('dataArray'));

    //留言
    $(".com-button").click(function(){
        var yh = $(".yh span").html();
        var textxin = UE.getEditor('container').getContent();
        var timestamp= (new Date()).valueOf()/1000;//把当前日期变成时间戳
        console.log(timestamp);
        if(!loginInfo){
            alert('未登录不可留言');
        } else {
            if(textxin == ''){
                alert('留言不可为空');
            }else {
                //成功留言
                 $.ajax({
                  type: "POST",
                  url: hrf+"index.php/sendMsg",
                  data: {"phone":loginInfo.phone,"accesstoken":loginInfo.accesstoken,"title":textxin},// 回头做
                  dataType: 'json',
                  success: function(data) {
                    console.log(data);
                    if(data.code == '200'){
                        //局部刷新
                    	UE.getEditor('container').setContent('');
                        $.ajax({
                            type: "POST",
                            url: hrf+"index.php/messageList",
                            dataType: 'json',
                            success: function(data) {
                                if(data.code == '200'){
                                    console.log(data.data.query_count);
                                    var leng = data.data.query_count;//data.data.res 
                                    var datajson = data.data.res;
                                    var e = "";
                                    var mdiv ='';
                                    var mpic = '<div>';
                                    if(leng>0){
                                        for (i = 0; i< leng;i++) {
                                            var avata = data.data.res[i].avatar;
                                                if(avata == ''){
                                                    avata = hrf+'images/wechatcur.png';
                                                }
                                                if(data.data.res[i].pic1 != ''&&data.data.res[i].pic1 !=undefined){
                                                if(data.data.res[i].pic9 != '' &&data.data.res[i].pic9 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic9+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic8 != ''&&data.data.res[i].pic8 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic8+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic7 != ''&&data.data.res[i].pic7 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic7+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic6 != ''&&data.data.res[i].pic6 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic6+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic5 != ''&&data.data.res[i].pic5 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic5+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic4 != ''&&data.data.res[i].pic4 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic4+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic3 != ''&&data.data.res[i].pic3 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic3+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic2 != ''&&data.data.res[i].pic2 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic2+'">'+mpic;
                                                }
                                                  mpic = '<img src="'+hrf+data.data.res[i].pic1+'">'+mpic;
                                                  mdiv ='<div class="mdiv">'+mpic+'</div>'
                                                }
                                            e +='<li>'+
                                                '<div class="com-img">'+
                                                    '<img src="'+
                                                    avata+
                                                    '">'+
                                                '</div>'+
                                                '<div class="com-p">'+
                                                    '<h5>'+
                                                    datajson[i].nickname +
                                                '<span>'+
                                                datajson[i].createtime +
                                                '</span></h5>'+
                                                    '<p>'+
                                                    datajson[i].title+
                                                '</p>'+
                                                mdiv+
                                                '</div><div class="clear"></div></li>';
                                                fpage = datajson[i].id;
                                                mdiv='';
                                                mpic='';
                                        };
                                        $('.comments-main ul').html(e);
                                }
                            }
                            },
                            error: function() {}
                        })
                    }else if(data.code == '201'){
                    	alert('抱歉，留言失败~喝口水再来吧');
                    }else if(data.code == '202'||data.code == '203'){
                    	alert('账号信息不能为空哟');
                    }else if(data.code == '205'){
                    	alert('没有登录可不能留言');
                    }else if(data.code == '206'){
                    	alert('你已被打入冷宫，不能发言了哟');
                    }
                    else {
                      alert('请重新留言');
                    }
                  },
                  error: function() {}
              })
            }
        }
    })

    //留言列表
    var fpage = '';//游标
    	$.ajax({
    		type: "POST",
    		url: hrf+"index.php/messageList",
    		dataType: 'json',
            success: function(data) {
            	if(data.code == '200'){
            		console.log(data.data.query_count);
            		var leng = data.data.query_count;//data.data.res 
            		var datajson = data.data.res;
                var e = "";
                	var mdiv ='';
                                    var mpic = '<div>';
                                    if(leng>0){
                                        for (i = 0; i< leng;i++) {
                                            var avata = data.data.res[i].avatar;
                                                if(avata == ''){
                                                    avata = hrf+'images/wechatcur.png';
                                                }
                                                if(data.data.res[i].pic1 != ''&&data.data.res[i].pic1 !=undefined){
                                                if(data.data.res[i].pic9 != '' &&data.data.res[i].pic9 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic9+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic8 != ''&&data.data.res[i].pic8 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic8+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic7 != ''&&data.data.res[i].pic7 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic7+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic6 != ''&&data.data.res[i].pic6 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic6+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic5 != ''&&data.data.res[i].pic5 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic5+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic4 != ''&&data.data.res[i].pic4 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic4+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic3 != ''&&data.data.res[i].pic3 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic3+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic2 != ''&&data.data.res[i].pic2 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic2+'">'+mpic;
                                                }
                                                  mpic = '<img src="'+hrf+data.data.res[i].pic1+'">'+mpic;
                                                  mdiv ='<div class="mdiv">'+mpic+'</div>'
                                                }
                                            e +='<li>'+
                                                '<div class="com-img">'+
                                                    '<img src="'+
                                                    avata+
                                                    '">'+
                                                '</div>'+
                                                '<div class="com-p">'+
                                                    '<h5>'+
                                                    datajson[i].nickname +
                                                '<span>'+
                                               datajson[i].createtime +
                                                '</span></h5>'+
                                                    '<p>'+
                                                    datajson[i].title+
                                                '</p>'+
                                                mdiv+
                                                '</div><div class="clear"></div></li>';
                                                fpage = datajson[i].id;
                                                mdiv='';
                                                mpic='';
                                        };
                		$('.comments-main ul').html(e);
            	}
        	}
        	else{
        		$('.comments-main ul').html('');
                $(".com-more").html('暂无更多留言');
        	}
            },
            error: function() {}
        })
	//more
     $("body").on("click", ".com-more", function() {
        $.ajax({
            type: "POST",
            url: hrf+"index.php/messageList",
            data: {"direction":2,"from":fpage},
            dataType: 'json',
            success: function(data) {
              if(data.code == '200'){
                console.log(data.data.query_count);
                var leng = data.data.query_count;//data.data.res 
                var datajson = data.data.res;
                var e = "";
                  var mdiv ='';
                                    var mpic = '<div>';
                                    if(leng>0){
                                        for (i = 0; i< leng;i++) {
                                            var avata = data.data.res[i].avatar;
                                                if(avata == ''){
                                                    avata = hrf+'images/wechatcur.png';
                                                }
                                                if(data.data.res[i].pic1 != ''&&data.data.res[i].pic1 !=undefined){
                                                if(data.data.res[i].pic9 != '' &&data.data.res[i].pic9 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic9+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic8 != ''&&data.data.res[i].pic8 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic8+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic7 != ''&&data.data.res[i].pic7 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic7+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic6 != ''&&data.data.res[i].pic6 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic6+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic5 != ''&&data.data.res[i].pic5 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic5+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic4 != ''&&data.data.res[i].pic4 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic4+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic3 != ''&&data.data.res[i].pic3 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic3+'">'+mpic;
                                                }
                                                if(data.data.res[i].pic2 != ''&&data.data.res[i].pic2 !=undefined){
                                                    mpic = '<img src="'+hrf+data.data.res[i].pic2+'">'+mpic;
                                                }
                                                  mpic = '<img src="'+hrf+data.data.res[i].pic1+'">'+mpic;
                                                  mdiv ='<div class="mdiv">'+mpic+'</div>'
                                                }
                                            e +='<li>'+
                                                '<div class="com-img">'+
                                                    '<img src="'+
                                                    avata+
                                                    '">'+
                                                '</div>'+
                                                '<div class="com-p">'+
                                                    '<h5>'+
                                                    datajson[i].nickname +
                                                '<span>'+
                                                datajson[i].createtime +
                                                '</span></h5>'+
                                                    '<p>'+
                                                    datajson[i].title+
                                                '</p>'+
                                                mdiv+
                                                '</div><div class="clear"></div></li>';
                                                fpage = datajson[i].id;
                                                mdiv='';
                                                mpic='';
                                        };
                    $('.comments-main ul').append(e);
                    if(leng<10){
                    $(".com-more").html('暂无更多留言');
              }
              }
              else if(leng>0){
                    $(".com-more").html('暂无更多留言');
              }
          }
          else{
                $(".com-more").html('暂无更多留言');
          }
            },
            error: function() {}
        })
    });
	
});